﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;

namespace eProject.Web.UI.Admin
{
    public partial class CompanyManagementProgram : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();
        }
        private void PopulateControls()
        {
            if (Page.IsPostBack) return;
            BindCompanySearch();
        }
        private void BindCompany()
        {
            try
            {
                grvCompany.DataSource = Company.GetAll();
                grvCompany.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        private void BindCompanySearch()
        {
            try
            {
                bool delete = cbDelete.Checked;
                string text = txtSearchText.Text.Trim().ToString();
                grvCompany.DataSource = Company.GetSearch(text,delete);
                grvCompany.DataBind();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        protected void grvCompany_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvCompany.PageIndex = e.NewPageIndex;
            grvCompany.DataBind();
            BindCompanySearch();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string text = txtSearchText.Text.Trim().ToString();
            bool delete = cbDelete.Checked;
            grvCompany.DataSource = Company.GetSearch(text,delete);
            grvCompany.DataBind();
        }
        protected void grvCompany_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = int.Parse(grvCompany.DataKeys[e.NewEditIndex].Value.ToString());
            Response.Redirect("CompanyManagementEdit.aspx?Id=" + id);
        }
        protected void grvCompany_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(grvCompany.DataKeys[e.RowIndex].Value.ToString());
            Company com = new Company(id);
            com.IsDeleted = true;
            com.Save();
            BindCompanySearch();
        }
        protected void grvCompany_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = int.Parse(grvCompany.DataKeys[e.RowIndex].Value.ToString());
            Company com = new Company(id);
            com.IsDeleted = false;
            com.Save();
            BindCompanySearch();
        }
    }
}